<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/25
 * Time: 14:20
 */

namespace Admin\Model;


use Common\Model\CommonModel;

class ProductCateModel extends CommonModel
{
    protected $tableName = 'project_cate';
    private $shop_product_cate_list_cache = 'sp_shop_product_cate_list_';

    /**
     * 判断商品分类是否存在
     * @param $shop_id
     * @param $title
     * @param string $id
     * @return bool
     */
    public function checkCateTitleExists($shop_id,$title,$id=''){
        $map = array();
        $map['shop_id'] = $shop_id;
        $map['title'] = $title;
        $id && $map['id'] = array('neq',$id);

        $res = $this->where($map)->getField($this->pk);

        if($res){
            return true;
        } else {
            return false;
        }
    }

    /**
     * 排序更新
     * @param $id
     * @param $sort
     * @return bool
     */
    public function setSort($id,$sort){
        return parent::setStatus($id,$sort,'sort');
    }

    /**
     * 获取商品分类下的商品数量，可用于判断分类下是否有商品
     * @param $id
     * @return int
     */
    public function hasProduct($id){
        if(empty($id)) return 0;
        return M('project')->where(array('cate_id'=>$id))->count();
    }

    /**
     * 获取店铺下的分类列表
     * @param $shop_id
     * @return bool|mixed
     */
    public function getShopCate($shop_id){
        if(empty($shop_id)) return false;
        $list = S($this->shop_product_cate_list_cache.$shop_id);
        if(!$list){
            $list = $this->where('shop_id='.$shop_id)->order('sort asc')->select();
            S($this->shop_product_cate_list_cache.$shop_id,$list);
        }
        return $list;
    }

    /**
     * 清理缓存
     * @param $shop_id
     */
    public function clearCache($shop_id){
        !empty($shop_id) && S($this->shop_product_cate_list_cache.$shop_id,null);
    }
}